﻿using System;
using System.Data.SqlClient;
using System.Globalization;
using Pantry.Commons;
using Pantry.DataAccess;
using System.Data;
using Pantry.Entity;

namespace Pantry.Business
{
    public class ServiceBus
    {
        private readonly ServiceDao _serviceDao;

        public ServiceBus()
        {
            _serviceDao = new ServiceDao();
        }

        /// <summary>
        /// Inserts the service.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <returns></returns>
        public int InsertService(Service service)
        {
            return _serviceDao.InsertService(service);
        }

        /// <summary>
        /// Updates the service.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <returns></returns>
        public int UpdateService(Service service)
        {
            return _serviceDao.UpdateService(service);
        }

        /// <summary>
        /// Selects all service.
        /// </summary>
        /// <returns></returns>
        public DataTable SelectAllService()
        {
            return _serviceDao.SelectAllService();
        }

        /// <summary>
        /// Selects the type of the service.
        /// </summary>
        /// <returns></returns>
        public DataTable SelectServiceType()
        {
            return _serviceDao.SelectServiceType();
        }

        /// <summary>
        /// Selects the type of the service sub.
        /// </summary>
        /// <returns></returns>
        public DataTable SelectServiceSubType()
        {
            return _serviceDao.SelectServiceSubType();
        }

        /// <summary>
        /// Selects the service attendance.
        /// </summary>
        /// <returns></returns>
        public DataTable SelectServiceAttendance()
        {
            return _serviceDao.SelectServiceAttendance();
        }

        /// <summary>
        /// Selects the referral process.
        /// </summary>
        /// <returns></returns>
        public DataTable SelectReferralProcess()
        {
            return _serviceDao.SelectReferralProcess();
        }

        /// <summary>
        /// Marks the activated.
        /// </summary>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public bool MarkActivated(bool isActive, int id)
        {
            try
            {
                var result = _serviceDao.MarkActivated(isActive, id);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }

        /// <summary>
        /// Checks the input date with current date.
        /// </summary>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="extendable">The extendable.</param>
        /// <returns></returns>
        public bool CheckInputDateWithCurrentDate(DateTime startDate, DateTime endDate, DateTime extendable)
        {
            try
            {
                int result1;
                int result2;
                DateTime currentDate = DateTime.Now;
                DateTime addEndDateWithExtendable = endDate.Add(TimeSpan.Parse(extendable.ToString(CultureInfo.InvariantCulture)));
                TimeSpan diff2 = addEndDateWithExtendable.Subtract(currentDate);
                if (string.IsNullOrEmpty(startDate.ToString(CultureInfo.InvariantCulture)))
                {
                    result1 = -1; // less than current date
                }
                if (string.IsNullOrEmpty(endDate.ToString(CultureInfo.InvariantCulture)) ||
                    string.IsNullOrEmpty(extendable.ToString(CultureInfo.InvariantCulture)))
                {
                    result2 = 1; // greater than current date
                }
                if (string.IsNullOrEmpty(startDate.ToString(CultureInfo.InvariantCulture)) &&
                    (string.IsNullOrEmpty(endDate.ToString(CultureInfo.InvariantCulture)) ||
                    string.IsNullOrEmpty(extendable.ToString(CultureInfo.InvariantCulture))))
                {
                    return true;
                }
                result1 = DateTime.Compare(startDate, currentDate);
                result2 = DateTime.Compare(addEndDateWithExtendable, currentDate);
                return result1 <= 0 && result2 >= 0;
            }
            catch (ApplicationException e)
            {
                ExceptionHandler.WriteErrorLog(e);
                return false;
            }
        }
        /// <summary>
        /// Selects the service id.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public DataTable SelectServiceId(string name)
        {
            return _serviceDao.SelectServiceId(name);
        }
        /// <summary>
        /// Copies the service.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Service SelectServiceById(int id)
        {
            return _serviceDao.SelectServiceById(id);
        }

        public bool CheckUniqueForAdd(string tableName, string fieldName, string fieldValue)
        {
            int result = new PantryBus().CheckUnique(tableName,fieldName,fieldValue);
            return (result == 0);
        }
        public bool CheckUniqueForUpdate(string table, string fieldName, string newValue, string fieldId, int valueId)
        {
            int result = new PantryBus().CheckUniqueForAmend(table,fieldName,newValue,fieldId,valueId);
            return (result == 0);
        }
    }
}
